<script setup>
import { ref, defineProps, defineEmits } from 'vue'

// 定义属性
const props = defineProps({
  show: {
    type: Boolean,
    default: false
  }
})

// 定义事件
const emit = defineEmits(['close', 'registerSuccess'])

const username = ref('')
const email = ref('')
const password = ref('')
const confirmPassword = ref('')
const errorMessage = ref('')
const successMessage = ref('')

// 处理注册表单提交
const handleRegister = (e) => {
  e.preventDefault()
  
  // 简单的表单验证
  if (!username.value || !email.value || !password.value) {
    errorMessage.value = '请填写所有必填字段'
    successMessage.value = ''
    return
  }
  
  // 邮箱格式验证
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  if (!emailRegex.test(email.value)) {
    errorMessage.value = '请输入有效的邮箱地址'
    successMessage.value = ''
    return
  }
  
  // 密码长度验证
  if (password.value.length < 6) {
    errorMessage.value = '密码长度至少为6位'
    successMessage.value = ''
    return
  }
  
  // 确认密码匹配验证
  if (password.value !== confirmPassword.value) {
    errorMessage.value = '两次输入的密码不一致'
    successMessage.value = ''
    return
  }
  
  // 在实际项目中，这里会发送注册请求到后端
  console.log('注册请求:', {
    username: username.value,
    email: email.value,
    password: password.value
  })
  
  // 模拟注册成功
  errorMessage.value = ''
  successMessage.value = '注册成功！'
  
  // 3秒后关闭弹窗并触发成功事件
  setTimeout(() => {
    // 触发注册成功事件
    emit('registerSuccess', {
      username: username.value,
      email: email.value
    })
    
    // 清空表单
    resetForm()
    
    // 关闭弹窗
    emit('close')
  }, 2000)
}

// 重置表单
const resetForm = () => {
  username.value = ''
  email.value = ''
  password.value = ''
  confirmPassword.value = ''
  errorMessage.value = ''
  successMessage.value = ''
}

// 关闭弹窗
const handleClose = () => {
  resetForm()
  emit('close')
}

// 点击遮罩层关闭弹窗
const handleOverlayClick = (e) => {
  if (e.target === e.currentTarget) {
    handleClose()
  }
}
</script>

<template>
  <!-- 恢复条件渲染，只有在show为true时才显示弹窗 -->
  <Teleport to="body">
    <div v-if="show" class="modal-overlay" @click="handleOverlayClick">
      <div class="modal-content">
        <div class="modal-header">
          <h2 class="modal-title">阴阳师 - 注册</h2>
          <button class="modal-close" @click="handleClose" aria-label="关闭">
            <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
              <path d="M18 6L6 18M6 6L18 18" stroke="#d4af37" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
          </button>
        </div>
        
        <form @submit="handleRegister">
          <div class="form-group">
            <label for="username">用户名</label>
            <input
              type="text"
              id="username"
              v-model="username"
              placeholder="请设置您的用户名"
              class="form-input"
              required
              autofocus
            />
          </div>
          
          <div class="form-group">
            <label for="email">邮箱</label>
            <input
              type="email"
              id="email"
              v-model="email"
              placeholder="请输入您的邮箱"
              class="form-input"
              required
            />
          </div>
          
          <div class="form-group">
            <label for="password">密码</label>
            <input
              type="password"
              id="password"
              v-model="password"
              placeholder="请设置您的密码（至少6位）"
              class="form-input"
              required
            />
          </div>
          
          <div class="form-group">
            <label for="confirmPassword">确认密码</label>
            <input
              type="password"
              id="confirmPassword"
              v-model="confirmPassword"
              placeholder="请再次输入密码"
              class="form-input"
              required
            />
          </div>
          
          <div class="agreement">
            <label class="agreement-label">
              <input type="checkbox" required />
              我已阅读并同意<a href="#" class="agreement-link">用户协议</a>和<a href="#" class="agreement-link">隐私政策</a>
            </label>
          </div>
          
          <button type="submit" class="register-submit-btn">立即注册</button>
          
          <div class="login-link">
            已有账号？<a href="#" @click.prevent="emit('close', 'login')">立即登录</a>
          </div>
        </form>
        
        <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
        <div v-if="successMessage" class="success-message">{{ successMessage }}</div>
      </div>
    </div>
  </Teleport>
</template>

<style scoped>
:root {
  --gold-primary: #d4af37;
  --gold-secondary: #b8860b;
  --bg-dark: rgba(0, 0, 0, 0.9);
  --bg-light: rgba(255, 255, 255, 0.05);
  --border-color: rgba(212, 175, 55, 0.3);
  --success-color: #4caf50;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 99999; /* 提高z-index确保在最顶层 */
  /* 移除动画以避免潜在的渲染问题 */
  opacity: 1;
  visibility: visible;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.modal-content {
  background: var(--bg-dark);
  border: 2px solid var(--border-color);
  border-radius: 12px;
  padding: 40px;
  width: 100%;
  max-width: 450px;
  box-shadow: 0 0 30px rgba(212, 175, 55, 0.2);
  backdrop-filter: blur(10px);
  /* 暂时移除动画以避免潜在的渲染问题 */
  position: relative;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.modal-title {
  color: var(--gold-primary);
  font-size: 24px;
  text-shadow: 0 0 10px rgba(212, 175, 55, 0.5);
  margin: 0;
}

.modal-close {
  background: none;
  border: none;
  color: var(--gold-primary);
  cursor: pointer;
  padding: 5px;
  transition: all 0.3s ease;
}

.modal-close:hover {
  transform: rotate(90deg);
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  color: #fff;
  margin-bottom: 8px;
  font-size: 14px;
}

.form-input {
  width: 100%;
  padding: 12px 15px;
  background: var(--bg-light);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: #fff;
  font-size: 14px;
  transition: all 0.3s ease;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: var(--gold-primary);
  box-shadow: 0 0 10px rgba(212, 175, 55, 0.3);
}

.form-input::placeholder {
  color: rgba(255, 255, 255, 0.5);
}

.agreement {
  margin-bottom: 25px;
}

.agreement-label {
  display: flex;
  align-items: flex-start;
  color: #fff;
  font-size: 14px;
  cursor: pointer;
}

.agreement-label input {
  margin-right: 8px;
  margin-top: 2px;
}

.agreement-link {
  color: var(--gold-primary);
  text-decoration: none;
  transition: color 0.3s ease;
}

.agreement-link:hover {
  color: var(--gold-secondary);
}

.register-submit-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, var(--gold-primary), var(--gold-secondary));
  border: none;
  border-radius: 6px;
  color: #000;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 10px rgba(212, 175, 55, 0.3);
}

.register-submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(212, 175, 55, 0.5);
}

.login-link {
  text-align: center;
  margin-top: 20px;
  color: #fff;
  font-size: 14px;
}

.login-link a {
  color: var(--gold-primary);
  text-decoration: none;
  transition: color 0.3s ease;
}

.login-link a:hover {
  color: var(--gold-secondary);
}

.error-message {
  background: rgba(255, 0, 0, 0.2);
  border: 1px solid rgba(255, 0, 0, 0.3);
  border-radius: 6px;
  color: #ff6b6b;
  padding: 10px;
  margin-top: 15px;
  text-align: center;
  font-size: 14px;
}

.success-message {
  background: rgba(76, 175, 80, 0.2);
  border: 1px solid rgba(76, 175, 80, 0.3);
  border-radius: 6px;
  color: var(--success-color);
  padding: 10px;
  margin-top: 15px;
  text-align: center;
  font-size: 14px;
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideIn {
  from {
    transform: translateY(-20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .modal-content {
    padding: 30px 20px;
    margin: 0 20px;
    max-width: none;
  }
  
  .modal-title {
    font-size: 20px;
  }
  
  .agreement-label {
    flex-direction: column;
  }
  
  .agreement-label input {
    margin-bottom: 5px;
  }
}
</style>